<?php
// .-----------------------------------------------------------------------------------
// | WE TRY THE BEST WAY 杭州博也网络科技有限公司
// |-----------------------------------------------------------------------------------
// | Author: 贝贝 <hebiduhebi@163.com>
// | Copyright (c) 2013-2016, http://www.itboye.com. All Rights Reserved.
// |-----------------------------------------------------------------------------------
namespace Admin\Controller;

use Admin\Api\LangApi;
use Shop\Api\CategoryApi;
use Shop\Api\CategoryPropApi;
use Shop\Api\CategoryPropLangApi;
use Shop\Api\CategoryPropvalueApi;

class CategoryPropController extends AdminController{
		
	public function index(){
		$cate_id = I('cate_id',-1);
        $map = array(
            'cate_id'=>$cate_id,
            'lang'=>I('lang','zh-cn'),
        );
        $result = apiCall(CategoryApi::GET_INFO,array(['id'=>$cate_id]));
        if($result['status']){
            $this->assign('cate',$result['info']);
        }
        $order = "sort desc";
        $result = apiCall(CategoryPropApi::QUERY_NO_PAGING,array($map,$order,false));
        if($result['status']){
            $this->assign('cate_id',$cate_id);
            $this->assign('lang',LangApi::getLangSupport());
            $this->assign('list',$result['info']);
        }else{
            LogRecord('INFO:'.$result['info'],'[FILE] '.__FILE__.' [LINE] '.__LINE__);
            $this->error(L('UNKNOWN_ERR'));
        }
        $this->display();
	}

	public function add(){
		$cate_id = I('cate_id',-1);
		if(IS_GET){

			$this->assign("lang_list",LangApi::getLangSupport());
			$this->assign('cate_id',$cate_id);
			$this->display();
		}else{
//			$name = I('name','');
//
//			if(empty($name)){
//				$this->error("属性不能为空！");
//			}
			$langs = I('post.cate_prop_lang',[]);
			$langs_value = [];
            $default_lang_value = "";
			foreach ($langs as $vo){
				$cate_lang_value = I('post.cate_prop_lang_'.md5($vo),'');
                if($vo == 'zh-cn'){
                    $default_lang_value = $cate_lang_value;
                }
                if(empty($cate_lang_value)){
                    $cate_lang_value = $default_lang_value;
                }
				array_push($langs_value,$cate_lang_value);
			}
			
			$model = M();
			$catePropApi = new CategoryPropApi();
			$propid = md5(time().$cate_id.rand(0,10000));
			$result = $catePropApi->addCatePropLang($propid,$cate_id,$langs,$langs_value);

			
			if($result['status']){
				$model->commit();
				$this->success("添加成功！",U('Admin/CategoryProp/index',array('cate_id'=>$cate_id)));
			}
			else{
				$model->rollback();
				$this->error($result['info']);
			}
			
		}
	}
	
	public function edit(){
		$cate_id = I('get.cate_id',-1);
		$propid = I('get.propid','');
		
		if(IS_GET){
			$result = apiCall(CategoryPropApi::QUERY_NO_PAGING,array(array('propid'=>$propid)));

			if($result['status']){
				$list = $result['info'];
				$tmp = [];
				foreach ($list as $vo){
					$tmp[$vo['lang']] = $vo['propname'];
				}

				$this->assign("list",$tmp);
				$this->assign("lang_list",LangApi::getLangSupport());
				$this->assign('cate_id',$cate_id);
			}

			$this->assign('propid',$propid);
			$this->display();
		}else{
			$langs = I('post.cate_prop_lang',[]);
			$langs_value = [];

            $default_lang_value = "";
            foreach ($langs as $vo){
                $cate_lang_value = I('post.cate_prop_lang_'.md5($vo),'');
                if($vo == 'zh-cn'){
                    $default_lang_value = $cate_lang_value;
                }
                if(empty($cate_lang_value)){
                    $cate_lang_value = $default_lang_value;
                }
                array_push($langs_value,$cate_lang_value);
            }

			$result = (new CategoryPropApi())->updateCatePropLang($propid,$cate_id,$langs,$langs_value);

			
			if($result['status']){
				$this->success("保存成功！",U('Admin/CategoryProp/index',array('cate_id'=>$cate_id)));
			}
			else{
				$this->error($result['info']);
			}
			
		}
	}

	
	public function delete(){
        $propid = I('get.propid','');
        $id = I('get.id','');
		$map = array('prop_id'=>$id);
		$result = apiCall(CategoryPropvalueApi::QUERY_NO_PAGING,array($map));
		if($result['status']){
			if(count($result['info']) > 0){
				$this->error("存在属性值，请先删除属性值！");				
			}

			$result = apiCall(CategoryPropApi::DELETE,array(array('propid'=>$propid)));
			if($result['status']){
				$this->success("删除成功！");
			}else{
				$this->error($result['info']);
			}
			
		}else{
			
		}
	}


	public function saveSort(){
        $cate_id = $this->_param('cate_id','');
        $id_arr = $this->_param('ids','');
        $catePropApi = (new CategoryPropApi());
        for($i=0;$i<count($id_arr);$i++){
            if(empty($id_arr[$i])) continue;
            $sort = $this->_param('sort_'.$id_arr[$i],0);
            $result = $catePropApi->save(['propid'=>$id_arr[$i]],['sort'=>$sort]);
        }

        $this->success('操作成功',U('Admin/CategoryProp/index',['cate_id'=>$cate_id]));
    }

}

